var floorCount = 21;

var elevator = {
	current: 1,
	status: 0,  // 0: stop, 1: up, 2: down
	upTask: [],
	downTask: [],
};

$(document).ready(function(){
	var insiderBtnRowAt = floorCount%2;
	$("#well").css("height", floorCount*36+"px");
	$("#insider").css("height", floorCount*36+"px");
	for (var i = floorCount; i > 0; i--) {

		// 渲染楼层上下按键
		var htmlTo = "<div class='btnsRow'><div style='float: left; width: 30px;'>"+i+"</div>";
		if (i != floorCount){
			htmlTo += "<a href='javascript:;'><div class='btn upBtn' floor='"+i+"' acc='1'>▲</div></a>";
		}
		if (i != 1) {
			htmlTo += "<a href='javascript:;'><div class='btn downBtn' floor='"+i+"' acc='2'>▼</div></a>";
		}
		$("#btnsGroup").append(htmlTo+"</div>");

		$("#downer").css("visibility", "hidden");
		$("#uper").css("visibility", "hidden");

		// 渲染电梯内部按键
		if (i % 2 == insiderBtnRowAt) {
			$("#insider").append("<div class='btnsRow'><a href='javascript:;'><div class='btn' floor='"+i+"' acc='0'>"+i+"</div></a>");
			if (i-1 != 0) {
				$("#insider").append("<a href='javascript:;'><div class='btn' floor='"+(i-1)+"' acc='0'>"+(i-1)+"</div></a>");
			}
			$("#insider").append("</div>");
		}

	}
	$(".btn[acc='0']").click(function(){
		$(this).css("color", "red");
		addTask($(this).attr("floor"));
	});

	$(".btn[acc='1']").click(function(){
		$(this).css("color", "red");
		addUpTask($(this).attr("floor"));
	});
	$(".btn[acc='2']").click(function(){
		$(this).css("color", "red");
		addDownTask($(this).attr("floor"));
	});

	// 初始化电梯状态
	$("#elevator").css("top", (floorCount - elevator.current)*36 + "px");
})

function addTask(floor){
	
	if (floor > elevator.current) {
		elevator.upTask.push(floor);
		elevator.upTask.sort(sortNumUp);
		if (elevator.status == 0) moveUp();
	}else{
		elevator.downTask.push(floor);
		elevator.downTask.sort(sortNumDown);
		if (elevator.status == 0) moveDown();
	}
	console.log(elevator.upTask);
	console.log(elevator.downTask);
}

function addUpTask(floor){
	if (elevator.status == 2) {
		if (floor > elevator.downTask[elevator.downTask.length-1]) {
			elevator.upTask.push(floor);
			elevator.upTask.sort(sortNumUp);
		}else{
			elevator.downTask.push(floor);
			elevator.downTask.sort(sortNumDown);
		}
	}
	if (floor > elevator.current) {
		elevator.upTask.push(floor);
		elevator.upTask.sort(sortNumUp);
		if (elevator.status == 0) moveUp();
	}else if (floor < elevator.current){
		elevator.downTask.push(floor);
		elevator.downTask.sort(sortNumDown);
		if (elevator.status == 0) moveDown();
	}else{
		if (elevator.status == 0) {
			$(".btn[floor='"+floor+"']").css("color", "black");
			doorOpen(function(){
				if(elevator.upTask.length == 0) {
					if (elevator.downTask.length != 0) moveDown();
					else movePause();
				}else moveUp();
			});
		}else{
			elevator.downTask.push(floor);
			elevator.downTask.sort(sortNumDown);
		}
	}
	
	// switch(elevator.status){

	// 	case 0:
	// 		if (floor > elevator.current) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 			moveUp();
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 			moveDown();
	// 		}
	// 		break;
	// 	case 1:
	// 		if (floor > elevator.current) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 		}
	// 		break;
	// 	case 2:
	// 		if (floor > elevator.current) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 		}
	// 		break;
	// 	default:
	// 		break;
	// }
	console.log(elevator.upTask);
	console.log(elevator.downTask);
}

function addDownTask(floor){
	// elevator.downTask.push(floor);
	// elevator.downTask.sort(sortNumDown);
	// if (elevator.status == 0) moveDown();
	if (elevator.status == 1) {
		if (floor > elevator.upTask[elevator.upTask.length-1]) {
			elevator.upTask.push(floor);
			// elevator.upTask.sort(sortNumUp);
		}else{
			elevator.downTask.push(floor);
			// elevator.downTask.sort(sortNumDown);
		}
	}else{
		if (floor > elevator.current) {
			elevator.upTask.push(floor);
			elevator.upTask.sort(sortNumUp);
			if (elevator.status == 0) moveUp();
		}else if (floor < elevator.current){
			elevator.downTask.push(floor);
			elevator.downTask.sort(sortNumDown);
			if (elevator.status == 0) moveDown();
		}else{
			if (elevator.status == 0) {
				$(".btn[floor='"+floor+"']").css("color", "black");
				doorOpen(function(){
					if(elevator.downTask.length == 0) {
						if (elevator.upTask.length != 0) moveUp();
						else movePause();
					}else moveDown();
				});
			}else{
				elevator.downTask.push(floor);
				elevator.downTask.sort(sortNumDown);
			}
		}
	}
	

	// switch(elevator.status){
	// 	case 0:
	// 		if (floor > elevator.current) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 			moveUp();
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 			moveDown();
	// 		}
	// 		break;
	// 	case 1:
	// 		if (floor > elevator.upTask[elevator.upTask.length-1]) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 		}
	// 		break;
	// 	case 2:
	// 		if (floor > elevator.downTask[0]) {
	// 			elevator.upTask.push(floor);
	// 			elevator.upTask.sort(sortNumUp);
	// 		}else{
	// 			elevator.downTask.push(floor);
	// 			elevator.downTask.sort(sortNumDown);
	// 		}
	// 		break;
	// 	default:
	// 		break;
	// }

	console.log(elevator.upTask);
	console.log(elevator.downTask);
}

// function moveTo(floor){
// 	if (floor > elevator.current) {
// 		elevator.status = 1;
// 		moveUp();
// 	}else{
// 		elevator.status = 2;
// 		moveDownFor(elevator.current - floor);
// 	}

// 	// var time = elevator.current >= floor ? elevator.current - floor : floor - elevator.current;
	

// }


function moveUp(){
	elevator.status = 1;
	$("#uper").css("visibility", "visible");
	$("#downer").css("visibility", "hidden");

	$("#elevator").animate({
		top: '-=36px'
	}, 1000, function(){
		elevator.current ++;
		$("#floorNum").html(elevator.current);
		
		if (elevator.upTask.length != 0) {
			if (elevator.current == elevator.upTask[0]) {
				$(".btn[floor='"+elevator.upTask[0]+"']").css("color", "black");
				elevator.upTask.shift();
				doorOpen(function(){
					if(elevator.upTask.length == 0) {
						if (elevator.downTask.length != 0) moveDown();
						else movePause();
					}else moveUp();
				});
				
			}else{
				moveUp();
			}
		}else{
			if (elevator.downTask.length != 0) moveDown();
			else movePause();
		}
		
	});
}

function moveDown(){
	elevator.status = 2;
	$("#downer").css("visibility", "visible");
	$("#uper").css("visibility", "hidden");

	$("#elevator").animate({
		top: '+=36px'
	}, 1000, function(){
		elevator.current --;
		$("#floorNum").html(elevator.current);
		

		if (elevator.downTask.length != 0) {
			if (elevator.current == elevator.downTask[0]) {
				$(".btn[floor='"+elevator.downTask[0]+"']").css("color", "black");
				elevator.downTask.shift();
				doorOpen(function(){
					if(elevator.downTask.length == 0) {
						if (elevator.upTask.length != 0) moveUp();
						else movePause();
					}else moveDown();
				});
				
			}else{
				moveDown();
			}
		}else{
			if (elevator.upTask.length != 0) moveUp();
			else movePause();
		}

	});
}

function movePause(){
	elevator.status = 0;
	$("#downer").css("visibility", "hidden");
	$("#uper").css("visibility", "hidden");
}

function doorOpen(finish){
	$("#elevator").css("background", "#ddd");
	setTimeout(function() {
		$("#elevator").css("background", "#ccc");
		finish();
	}, 3000);
}

function sortNumUp(a, b){
	return a-b
}
function sortNumDown(a, b){
	return b-a
}